package com.hnucm.cooklife.presenter.upload;

import androidx.annotation.NonNull;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.DishDetail;
import com.hnucm.cooklife.bean.uploaddish.UpRece;
import com.hnucm.cooklife.contract.uploaddish.Contract;
import com.hnucm.cooklife.model.UploadDishModel;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class UploadDishPresenter extends BasePresenter<Contract.IUploadDishView> implements Contract.IUploadDishPresenter {

    Contract.IUploadDishModel mIUploadDishModel;

    public UploadDishPresenter() {
        mIUploadDishModel = new UploadDishModel();
    }

    @Override
    public void uploadDish(RequestBody dishDetail) {
        mIUploadDishModel.uploadDish(dishDetail)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UpRece>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull UpRece upRece) {
                        if(isViewAttached()){
                            getView().onUploadDish(upRece);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage());
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
